package tools.FutureMode;

/**
 * @author Kevin
 * @date 2020/4/14 20:31
 */
public class FutureData implements Data {
    //FutureData是RealData的包装
    protected RealData realData = null;
    protected volatile boolean isReady = false;
    //static方法：Class锁；实例方法：this对象锁
    public synchronized void setRealData(RealData realData) {
        if (isReady) {
            return;
        }
        this.realData = realData;
        isReady = true;
        notifyAll();
    }

    //等待RealData构造完成
    @Override
    public synchronized String getResult() {
        while (!isReady) {
            try {//一直等待，直到RealData构造完成
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return realData.result;
    }

}
